utils.js ➔ validate   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
1
import cottus, { ValidationError } from 'cottus';
2
import { VALIDATION_FAILED } from './Error';
3
4
export function validate(data, rules) {
5
    const validator = cottus.compile([ 'required', { 'attributes': rules } ]);
6
7
    try {
8
        return validator.validate(data);
9
    } catch (error) {
10
        if (error instanceof ValidationError) {
11
            throw new VALIDATION_FAILED(error);
12
        }
13
14
        throw error;
15
    }
16
}
17